table.insert(Assets, Asset("ATLAS", "images/hud/buffslot1.xml"))
table.insert(Assets, Asset("ATLAS", "images/hud/buffslot2.xml"))

for i = 1, 4 do
    table.insert(Assets, Asset("ANIM", "anim/homura_weapon_ui_"..i..".zip"))
end

local containers = require "containers"
local params = containers.params

local HOMURA_CONTAINERS = {
    homura_rpg = 1,
    homura_pistol = 2,
    homura_gun = 3,
    homura_hmg = 3,
    homura_rifle = 4,
}

local STACKABLE = {
    homura_weapon_buff_ice = true,
    homura_weapon_buff_wind = true,
    homura_weapon_buff_flyingspeed = true,
    homura_weapon_buff_magic = true,
}

local test = function(container, item, slot)
    if item.prefab == "homura_weapon_buff_eye_lens" then
        print(container:GetNumSlots(), slot)
        return container:GetNumSlots() == 4 and slot == 1
    end

    if item:HasTag("homuraTag_buff") then
        if STACKABLE[item.prefab or 1] then
            return true
        else
            for i = 1, container:GetNumSlots()do
                if i ~= slot then
                    local buff = container:GetItemInSlot(i)
                    if buff ~= nil and buff.prefab == item.prefab then
                        return false
                    end
                end
            end
            return true
        end
    end
end

local tilepositions = {
    {Vector3(0,0,0)},
    {Vector3(43,-13,0),Vector3(-37,-13,0)},
    {Vector3(-80,10,0),Vector3(0,10,0),Vector3(80,10,0)},

    {Vector3(-150,10,0),Vector3(-60,10,0),Vector3(20,10,0),Vector3(100,10,0)}
}

local texture = { image = "buffslot2.tex", atlas = 'images/hud/buffslot2.xml' }
for k,v in pairs(HOMURA_CONTAINERS)do
    params[k] = {
        widget =  {
            slotpos = tilepositions[v],
            -- animbank = "homura_weapon_ui_"..v,
            animbank = "homura_weapon_ui_"..v,
            animbuild = "homura_weapon_ui_"..v,
            pos = Vector3(0, 100, 0),
            side_align_tip = 160,
            slotbg = {texture, texture, texture, texture},
        },

        acceptsstacks = false,
        canbeopened = false,
        type = "homura_weapon",
        itemtestfn = test,
    }
end

local container = require "components/container"
local old_drop = container.DropItem
container.DropItem = function(self, item, ...)
    if self.inst.prefab == "homura_rifle" and item and item.prefab == "homura_weapon_buff_eye_lens" then
        return
    else
        return old_drop(self, item, ...)
    end
end

local container_replica = require "components/container_replica"
for _,v in ipairs{"TakeActiveItemFromHalfOfSlot", "TakeActiveItemFromAllOfSlot"}do
    local old_take = container_replica[v]
    container_replica[v] = function(self, slot, ...)
        if self.inst.prefab == "homura_rifle" and slot == 1 then
            return
        else
            return old_take(self, slot, ...)
        end
    end
end

-- local HOMURA_CONTAINERS = {
--     homura_rpg      = "homura_weapon_ui_1",
--     homura_pistol   = "homura_weapon_ui_2",
--     homura_gun      = "homura_weapon_ui_3",
--     homura_hmg      = "homura_weapon_ui_3",
-- }

-- do
--     local old_setup = containers.widgetsetup
--     function containers.widgetsetup(container, prefab, data, ...)
--         local container_name = HOMURA_CONTAINERS[container.inst.prefab or ""]
--         if container_name ~= nil then
--             return old_setup(container, prefab, params[container_name])
--         else
--             return old_setup(container, prefab, data, ...)
--         end
--     end
-- end

-- do
--     local test = function(container, item, slot) return item:HasTag('homuraTag_buff') end
--     local tilepositions = {
--         [1] = {Vector3(0,0,0)},
--         [2] = {Vector3(43,-13,0),Vector3(-37,-13,0)},
--         [3] = {Vector3(-80,10,0),Vector3(0,10,0),Vector3(80,10,0)},
--     }
--     local texture = { image = "buffslot2.tex", atlas = 'images/hud/buffslot2.xml' }
--     for i = 1,3 do
--         params['homura_weapon_ui_'..i] =
--         {
--             widget =
--             {
--                 slotpos = tilepositions[i],
--                 animbank = "homura_weapon_ui_"..i,
--                 animbuild = "homura_weapon_ui_"..i,
--                 pos = Vector3(0, 100, 0),
--                 side_align_tip = 160,
--                 slotbg = {texture, texture, texture},
--             },

--             acceptsstacks = false,
--             canbeopened = false,
--             type = "homura_weapon",
--             itemtestfn = test,
--         }
--     end
-- end

-- -- 避免其他mod乱改 containers.widgetsetup 函数引起冲突
-- do
--     local self = require "components/container_replica"
--     local old_ctor = self._ctor
--     function self._ctor(self, inst, ...)
--         old_ctor(self, inst, ...)
--         print("client ctor")
--         if not TheWorld.ismastersim and self.widget == nil then
--             local container_name = HOMURA_CONTAINERS[inst.prefab or ""]
--             if container_name ~= nil then
--                 print("Homura-Warning: container_replica init failed!")
--                 containers.widgetsetup(self, container_name, params[container_name])
--             end
--         end
--     end

--     local old_setup = self.WidgetSetup
--     function self:WidgetSetup(prefab, data, ...)
--         containers.widgetsetup(self, prefab, data, ...)
--         if self.widget == nil then 
--             local container_name = HOMURA_CONTAINERS[self.inst.prefab or prefab or ""]
--             if container_name ~= nil then
--                 print("Homura-Warning: container_replica init failed!")
--                 containers.widgetsetup(self, container_name, params[container_name])
--             end
--         end
--         return old_setup(self, prefab, data, ...)
--     end
-- end
